/**
 * @param {Object} EventEmitter 发布对象
 * @description 
 *	发布/订阅模式类似于观察者模式，但它使用一个名为“发布者”的中介来管理观察者和被观察者之间的通信。
 */
class EventEmitter {
	constructor() {
		this.events = {};
	}

	on(event, listener) { //向发布者对象注册观察者
		if (!this.events[event]) {
			this.events[event] = [];
		}
		this.events[event].push(listener);
	}

	emit(event, data) { //向观察者对象发送事件
		let listeners = this.events[event];
		if (listeners) {
			for (let listener of listeners) {
				listener(data);
			}
		}
	}

	off(event, listener) { //注销观察者
		let listeners = this.events[event];
		if (listeners) {
			let index = listeners.indexOf(listener);
			if (index !== -1) {
				listeners.splice(index, 1);
			}
		}
	}
}

let emitter = new EventEmitter();
let listener1 = (data) => console.log(`Event 1: ${data}`);
let listener2 = (data) => console.log(`Event 2: ${data}`);

emitter.on("event1", listener1);
emitter.on("event2", listener2);

emitter.emit("event1", "some data");
emitter.emit("event2", "other data");

emitter.off("event1", listener1);

emitter.emit("event1", "more data");
// 输出：
// Event 1: some data
// Event 2: other data